<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,700,700italic%7CZCOOL+QingKe+HuangYou:300,300italic,400,400italic,700,700italic%7CNoto+Sans+Simplified+Chinese:300,300italic,400,400italic,700,700italic%7CJetBrains+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/lib/@fortawesome/fontawesome-free/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/animate.css/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/@fancyapps/ui/dist/fancybox/fancybox.css" integrity="sha256-RvRHGSuWAxZpXKV9lLDt2e+rZ+btzn48Wp4ueS3NZKs=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wyatex.online","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.18.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":true}}</script><script src="/js/config.js"></script>

    <meta name="description" content="可能很多人学完了 TS 之后想使用 vue 进行开发，但开发过程中可能会遇到非常多的问题，甚至有些在网上都找不到解决方法，这里总结一些比较常见的知识点，看完之后相信你也能快速上手 vue-ts 开发。 如果还没学过 TS 基础的小伙伴简易先根据TypeScript目录里的内容学习一下 TS 基础语法，当然除了 TS 之外还需要学习一下 Vue3 的基础语法。下面的所有用例都是基于 setup 语法">
<meta property="og:type" content="article">
<meta property="og:title" content="ts笔记（7）：使用TS编进行Vue开发的常见知识点">
<meta property="og:url" content="https://wyatex.online/TypeScript/ts%E7%AC%94%E8%AE%B0%EF%BC%887%EF%BC%89%EF%BC%9A%E4%BD%BF%E7%94%A8TS%E7%BC%96%E8%BF%9B%E8%A1%8CVue%E5%BC%80%E5%8F%91%E7%9A%84%E5%B8%B8%E8%A7%81%E7%9F%A5%E8%AF%86%E7%82%B9/index.html">
<meta property="og:site_name" content="Wyatex&#96;s blog">
<meta property="og:description" content="可能很多人学完了 TS 之后想使用 vue 进行开发，但开发过程中可能会遇到非常多的问题，甚至有些在网上都找不到解决方法，这里总结一些比较常见的知识点，看完之后相信你也能快速上手 vue-ts 开发。 如果还没学过 TS 基础的小伙伴简易先根据TypeScript目录里的内容学习一下 TS 基础语法，当然除了 TS 之外还需要学习一下 Vue3 的基础语法。下面的所有用例都是基于 setup 语法">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-07-26T11:37:40.000Z">
<meta property="article:modified_time" content="2023-07-25T00:59:59.000Z">
<meta property="article:author" content="Wyatex">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="学习笔记">
<meta property="article:tag" content="Vue3">
<meta property="article:tag" content="TypeScript">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://wyatex.online/TypeScript/ts%E7%AC%94%E8%AE%B0%EF%BC%887%EF%BC%89%EF%BC%9A%E4%BD%BF%E7%94%A8TS%E7%BC%96%E8%BF%9B%E8%A1%8CVue%E5%BC%80%E5%8F%91%E7%9A%84%E5%B8%B8%E8%A7%81%E7%9F%A5%E8%AF%86%E7%82%B9/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://wyatex.online/TypeScript/ts%E7%AC%94%E8%AE%B0%EF%BC%887%EF%BC%89%EF%BC%9A%E4%BD%BF%E7%94%A8TS%E7%BC%96%E8%BF%9B%E8%A1%8CVue%E5%BC%80%E5%8F%91%E7%9A%84%E5%B8%B8%E8%A7%81%E7%9F%A5%E8%AF%86%E7%82%B9/","path":"TypeScript/ts笔记（7）：使用TS编进行Vue开发的常见知识点/","title":"ts笔记（7）：使用TS编进行Vue开发的常见知识点"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>ts笔记（7）：使用TS编进行Vue开发的常见知识点 | Wyatex`s blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Wyatex`s blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">97</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">26</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">155</span></a></li><li class="menu-item menu-item-编程"><a href="/programming/" rel="section"><i class="fa fa-book fa-fw"></i>编程</a></li><li class="menu-item menu-item-蹲坑读物"><a href="/%E5%B9%B2%E8%B4%A7/%E5%BC%80%E5%8F%91%E4%BA%A4%E6%B5%81%E7%BE%A4%E5%88%86%E4%BA%AB%E8%AE%B0%E5%BD%95/" rel="section"><i class="fa fa-book fa-fw"></i>蹲坑读物</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%A2%A6%E7%9A%84%E5%BC%80%E5%A7%8B"><span class="nav-number">1.</span> <span class="nav-text">梦的开始</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%BB%99%E5%93%8D%E5%BA%94%E5%BC%8F%E6%8F%90%E4%BE%9B%E7%B1%BB%E5%9E%8B"><span class="nav-number">2.</span> <span class="nav-text">给响应式提供类型</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#setup-%E7%BC%96%E8%AF%91%E5%AE%8F"><span class="nav-number">3.</span> <span class="nav-text">setup 编译宏</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#defineProps"><span class="nav-number">3.1.</span> <span class="nav-text">defineProps</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#defineEmits"><span class="nav-number">3.2.</span> <span class="nav-text">defineEmits</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#defineExpose"><span class="nav-number">3.3.</span> <span class="nav-text">defineExpose</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#defineSlots"><span class="nav-number">3.4.</span> <span class="nav-text">defineSlots</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#defineOptions"><span class="nav-number">3.5.</span> <span class="nav-text">defineOptions</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#defineModel"><span class="nav-number">3.6.</span> <span class="nav-text">defineModel</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Wyatex"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Wyatex</p>
  <div class="site-description" itemprop="description">这里是我用来做笔记的网站哟~</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">155</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">97</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wyatex" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wyatex" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wyatex@qq.com" title="E-Mail → mailto:wyatex@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://antfu.me/" title="https:&#x2F;&#x2F;antfu.me&#x2F;" rel="noopener" target="_blank">Anthony Fu</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://www.codesky.me/" title="https:&#x2F;&#x2F;www.codesky.me&#x2F;" rel="noopener" target="_blank">CodeSky</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://learn.lianglianglee.com/" title="https:&#x2F;&#x2F;learn.lianglianglee.com&#x2F;" rel="noopener" target="_blank">技术文章摘抄</a>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        <div class="sidebar-inner sidebar-post-related">
          <div class="animated">
              <div class="links-of-blogroll-title"><i class="fa fa-signs-post fa-fw"></i>
    相关文章
  </div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/Vue3/Vue3%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%884%EF%BC%89%EF%BC%9Asetup/" rel="bookmark">
        <time class="popular-posts-time">2021-10-15</time>
        <br>
      Vue3学习笔记（4）：setup函数
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/Vue3/Vue3%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%885%EF%BC%89%EF%BC%9Ajsx/" rel="bookmark">
        <time class="popular-posts-time">2021-10-17</time>
        <br>
      Vue3学习笔记（5）：jsx
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/JavaScript/const-%E5%92%8C-Object-freeze-%E7%9A%84%E5%8C%BA%E5%88%AB/" rel="bookmark">
        <time class="popular-posts-time">2020-11-19</time>
        <br>
      const 和 Object.freeze() 的区别
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/TypeScript/ts%E7%AC%94%E8%AE%B0%EF%BC%881%EF%BC%89%EF%BC%9A%E6%90%AD%E5%BB%BA%E5%92%8C%E4%B8%80%E4%BA%9B%E5%9F%BA%E7%A1%80%E7%B1%BB%E5%9E%8B/" rel="bookmark">
        <time class="popular-posts-time">2021-10-28</time>
        <br>
      ts笔记（1）：搭建和一些基础类型
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/Vue3/Vue3%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%882%EF%BC%89%EF%BC%9AProps/" rel="bookmark">
        <time class="popular-posts-time">2021-10-14</time>
        <br>
      Vue3学习笔记（2）：Props
      </a>
    </li>
  </ul>

          </div>
        </div>
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wyatex.online/TypeScript/ts%E7%AC%94%E8%AE%B0%EF%BC%887%EF%BC%89%EF%BC%9A%E4%BD%BF%E7%94%A8TS%E7%BC%96%E8%BF%9B%E8%A1%8CVue%E5%BC%80%E5%8F%91%E7%9A%84%E5%B8%B8%E8%A7%81%E7%9F%A5%E8%AF%86%E7%82%B9/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Wyatex">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Wyatex`s blog">
      <meta itemprop="description" content="这里是我用来做笔记的网站哟~">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="ts笔记（7）：使用TS编进行Vue开发的常见知识点 | Wyatex`s blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ts笔记（7）：使用TS编进行Vue开发的常见知识点
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-07-26 19:37:40" itemprop="dateCreated datePublished" datetime="2022-07-26T19:37:40+08:00">2022-07-26</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-07-25 08:59:59" itemprop="dateModified" datetime="2023-07-25T08:59:59+08:00">2023-07-25</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/TypeScript/" itemprop="url" rel="index"><span itemprop="name">TypeScript</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>可能很多人学完了 TS 之后想使用 vue 进行开发，但开发过程中可能会遇到非常多的问题，甚至有些在网上都找不到解决方法，这里总结一些比较常见的知识点，看完之后相信你也能快速上手 vue-ts 开发。</p>
<p>如果还没学过 TS 基础的小伙伴简易先根据<a href="/categories/TypeScript/">TypeScript</a>目录里的内容学习一下 TS 基础语法，当然除了 TS 之外还需要学习一下 Vue3 的基础语法。下面的所有用例都是基于 setup 语法糖进行讲解和演示。</p>
<span id="more"></span>

<h1 id="梦的开始"><a href="#梦的开始" class="headerlink" title="梦的开始"></a>梦的开始</h1><p>既然需要学习 vue 和 ts，那么使用 vite 的推荐项目模板是最方便的，而且没有其他杂七杂八的东西干扰你：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">pnpm create vite</span><br><span class="line"># 创建个vue-ts-test项目 选择vue-ts</span><br><span class="line">cd vue-ts-test</span><br><span class="line">pnpm install</span><br><span class="line">pnpm run dev</span><br></pre></td></tr></table></figure>

<p>这时候整个项目就跑起来了，接下来就开始正式学习 ts</p>
<h1 id="给响应式提供类型"><a href="#给响应式提供类型" class="headerlink" title="给响应式提供类型"></a>给响应式提供类型</h1><p>可以先打开 <code>src/components/HelloWorld.vue</code>，看到第 6 行（未来可能会变）：<code>const count = ref(0)</code>。在 vscode 编辑器或者 webstorm 打开后，把鼠标移到 count 可以看到：<code>const count: Ref&lt;number&gt;</code>，说明这是一个响应式 ref 变量而且 value 的类型是 number。这是 ref 函数的类型推导功能，一般来说传入任何基础类型字面量都能推导出正确的类型。</p>
<p>现在有个需求，可能你的 count 不仅需要存一个 number，还有可能是 string，那么只需要改成：<code>const count = ref&lt;string | number&gt;(0)</code>，可以看出这是一个泛型，传入的类型都会作为 value 的类型。还有个注意点是如果 ref 不传参的话 value 还有可能是 undefined，比如这样写：<code>const count = ref&lt;number&gt;()</code>，类型推导得出：<code>const count: Ref&lt;number | undefined&gt;</code></p>
<p>vue3 除了 ref 之外还能使用 reactive 方法实现响应式，不过 reactice 一般是传入一个对象，所以一般会这样用：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">Obj</span> &#123;</span><br><span class="line">  name?: <span class="built_in">string</span>;</span><br><span class="line">  <span class="attr">age</span>: <span class="built_in">number</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> <span class="attr">obj</span>: <span class="title class_">Obj</span> = <span class="title function_">reactive</span>(&#123;</span><br><span class="line">  <span class="attr">age</span>: <span class="number">123</span>,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>除了这两个方法之外，vue3 还有一个非常好用的工具：computed 方法，它也有类型推导，使用方法如下：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 推导得到的类型：ComputedRef&lt;number&gt;</span></span><br><span class="line"><span class="keyword">const</span> doubleCount = <span class="title function_">computed</span>(<span class="function">() =&gt;</span> count.<span class="property">value</span> * <span class="number">2</span>);</span><br><span class="line"><span class="comment">// =&gt; TS Error: Property &#x27;split&#x27; does not exist on type &#x27;number&#x27;</span></span><br><span class="line"><span class="keyword">const</span> result = double.<span class="property">value</span>.<span class="title function_">split</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line"><span class="comment">// 还可以通过泛型参数指定返回类型</span></span><br><span class="line"><span class="keyword">const</span> double = computed&lt;<span class="built_in">number</span>&gt;(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 若返回值不是 number 类型则会报错</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h1 id="setup-编译宏"><a href="#setup-编译宏" class="headerlink" title="setup 编译宏"></a>setup 编译宏</h1><h2 id="defineProps"><a href="#defineProps" class="headerlink" title="defineProps"></a>defineProps</h2><p>同样看到<code>src/components/HelloWorld.vue</code>的第 4 行，<code>defineProps&lt;&#123; msg: string &#125;&gt;()</code>，在 setup 语法糖中，使用一个 defineProps 宏函数定义组件的类型，相当于 vue2 中的：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">props</span>: &#123;</span><br><span class="line">    <span class="attr">msg</span>: &#123;</span><br><span class="line">      <span class="attr">type</span>: <span class="title class_">String</span>,</span><br><span class="line">      <span class="attr">require</span>: <span class="literal">true</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>当然如果想让 msg 可以不传入的话，可以这样：<code>defineProps&lt;&#123; msg?: string &#125;&gt;()</code></p>
<p>如果还需要默认值的话可以这样：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">withDefaults</span>(defineProps&lt;&#123; msg?: <span class="built_in">string</span> &#125;&gt;(), &#123;</span><br><span class="line">  <span class="attr">msg</span>: <span class="string">&quot;hello&quot;</span>,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>如果你需要在 setup 中拿到 props 的值可以这样：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> props = defineProps&lt;&#123; <span class="attr">age</span>: <span class="built_in">string</span> &#125;&gt;(); <span class="comment">// withDefaults也是一样</span></span><br><span class="line"><span class="keyword">const</span> myComputed = <span class="title function_">computed</span>(<span class="function">() =&gt;</span> props.<span class="property">age</span> * <span class="number">2</span>);</span><br><span class="line"><span class="comment">// 这样myComputed可以响应式的根据传入的props进行计算</span></span><br></pre></td></tr></table></figure>

<p>vue3.3 版本之后，defineProps 可以接受泛型：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"> //-------------父组件-----------------</span><br><span class="line"> &lt;Child :name=&quot;[&#x27;xiaoman&#x27;]&quot;&gt;&lt;/Child&gt;</span><br><span class="line"></span><br><span class="line"> //-------------子组件-----------------</span><br><span class="line"> &lt;template&gt;</span><br><span class="line"> &lt;div&gt;</span><br><span class="line">     &#123;&#123; name &#125;&#125;</span><br><span class="line"> &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"> &lt;script generic=&quot;T&quot;  lang=&#x27;ts&#x27; setup&gt;</span><br><span class="line"> defineProps&lt;&#123;</span><br><span class="line">    name:T[]</span><br><span class="line"> &#125;&gt;()</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>这样，这里的 T 就是 <code>string[]</code> 类型了</p>
<h2 id="defineEmits"><a href="#defineEmits" class="headerlink" title="defineEmits"></a>defineEmits</h2><p>子组件定义 emit 类型：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> emit = <span class="title function_">defineEmits</span>([<span class="string">&quot;send&quot;</span>]);</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">send</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="comment">// 通过派发事件，将数据传递给父组件</span></span><br><span class="line">  <span class="title function_">emit</span>(<span class="string">&quot;send&quot;</span>, <span class="string">&quot;我是子组件的数据&quot;</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>父组件监听事件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line"> &lt;div&gt;</span><br><span class="line">    &lt;Child @send=&quot;getName&quot;&gt;&lt;/Child&gt;</span><br><span class="line"> &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"> &lt;script lang=&#x27;ts&#x27; setup&gt;</span><br><span class="line"> import Child from &#x27;./views/child.vue&#x27;</span><br><span class="line"> const getName = (name: string) =&gt; &#123;</span><br><span class="line">     console.log(name)</span><br><span class="line"> &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>子组件 TS 字面量模式派发</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> emit = defineEmits&lt;&#123;</span><br><span class="line">  (<span class="attr">event</span>: <span class="string">&quot;send&quot;</span>, <span class="attr">name</span>: <span class="built_in">string</span>): <span class="built_in">void</span>;</span><br><span class="line">&#125;&gt;();</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">send</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="comment">// 通过派发事件，将数据传递给父组件</span></span><br><span class="line">  <span class="title function_">emit</span>(<span class="string">&quot;send&quot;</span>, <span class="string">&quot;我是子组件的数据&quot;</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>Vue3.3 新写法更简短</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> emit = defineEmits&lt;&#123;</span><br><span class="line">  <span class="attr">send</span>: [<span class="attr">name</span>: <span class="built_in">string</span>];</span><br><span class="line">&#125;&gt;();</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">send</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="comment">// 通过派发事件，将数据传递给父组件</span></span><br><span class="line">  <span class="title function_">emit</span>(<span class="string">&quot;send&quot;</span>, <span class="string">&quot;我是子组件的数据&quot;</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="defineExpose"><a href="#defineExpose" class="headerlink" title="defineExpose"></a>defineExpose</h2><p>导出一些组件内的内容</p>
<figure class="highlight ts"><figcaption><span>Child.vue</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">defineExpose</span>(&#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;张三&quot;</span>,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>父组件：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line"> &lt;div&gt;</span><br><span class="line">    &lt;child ref=&quot;child&quot;&gt;派发事件&lt;/child&gt;</span><br><span class="line"> &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;script lang=&#x27;ts&#x27; setup&gt;</span><br><span class="line">import Child from &#x27;./views/child.vue&#x27;</span><br><span class="line">const child = ref&lt;InstanceType&lt;typeof Child&gt;&gt;();</span><br><span class="line">const getName = () =&gt; &#123;</span><br><span class="line">    console.log(child.value?.name)</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="defineSlots"><a href="#defineSlots" class="headerlink" title="defineSlots"></a>defineSlots</h2><p>子组件 defineSlots 只做声明不做实现 同时约束 slot 类型</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line"> &lt;div&gt;</span><br><span class="line">     &lt;ul&gt;</span><br><span class="line">        &lt;li v-for=&quot;(item,index) in data&quot;&gt;</span><br><span class="line">            &lt;slot :index=&quot;index&quot; :item=&quot;item&quot;&gt;&lt;/slot&gt;</span><br><span class="line">        &lt;/li&gt;</span><br><span class="line">     &lt;/ul&gt;</span><br><span class="line"> &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"> &lt;script generic=&quot;T&quot;  lang=&#x27;ts&#x27; setup&gt;</span><br><span class="line">defineProps&lt;&#123;</span><br><span class="line">    data: T[]</span><br><span class="line">&#125;&gt;()</span><br><span class="line">defineSlots&lt;&#123;</span><br><span class="line">   default(props:&#123;item:T,index:number&#125;):void</span><br><span class="line">&#125;&gt;()</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;Child :data=&quot;list&quot;&gt;</span><br><span class="line">            &lt;template #default=&quot;&#123;item&#125;&quot;&gt;</span><br><span class="line">                   &lt;div&gt;&#123;&#123; item.name &#125;&#125;&lt;/div&gt;</span><br><span class="line">            &lt;/template&gt;</span><br><span class="line">        &lt;/Child&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;script lang=&#x27;ts&#x27; setup&gt;</span><br><span class="line">import Child from &#x27;./views/child.vue&#x27;</span><br><span class="line">const list = [</span><br><span class="line">    &#123;</span><br><span class="line">        name: &quot;张三&quot;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        name: &quot;李四&quot;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        name: &quot;王五&quot;</span><br><span class="line">    &#125;</span><br><span class="line">]</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="defineOptions"><a href="#defineOptions" class="headerlink" title="defineOptions"></a>defineOptions</h2><p>Vue3.3 新指令，常用的就是定义 name 在 seutp 语法糖模式发现 name 不好定义了需要在开启一个 script 自定义 name 现在有了 defineOptions 就可以随意定义 name 了</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">defineOptions</span>(&#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;Child&quot;</span>,</span><br><span class="line">  <span class="attr">inheritAttrs</span>: <span class="literal">false</span>,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h2 id="defineModel"><a href="#defineModel" class="headerlink" title="defineModel"></a>defineModel</h2><p>由于该API处于实验性特性 可能会被删除暂时不讲</p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Wyatex
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://wyatex.online/TypeScript/ts%E7%AC%94%E8%AE%B0%EF%BC%887%EF%BC%89%EF%BC%9A%E4%BD%BF%E7%94%A8TS%E7%BC%96%E8%BF%9B%E8%A1%8CVue%E5%BC%80%E5%8F%91%E7%9A%84%E5%B8%B8%E8%A7%81%E7%9F%A5%E8%AF%86%E7%82%B9/" title="ts笔记（7）：使用TS编进行Vue开发的常见知识点">https://wyatex.online/TypeScript/ts笔记（7）：使用TS编进行Vue开发的常见知识点/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag"># 前端</a>
              <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag"># 学习笔记</a>
              <a href="/tags/Vue3/" rel="tag"># Vue3</a>
              <a href="/tags/TypeScript/" rel="tag"># TypeScript</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/uncategorized/Webpack5%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="prev" title="Webpack5学习笔记">
                  <i class="fa fa-angle-left"></i> Webpack5学习笔记
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/uncategorized/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%EF%BC%881%EF%BC%89%EF%BC%9AJest/" rel="next" title="单元测试（1）Jest">
                  单元测试（1）Jest <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2019 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Wyatex</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/ribbon.js/dist/ribbon.min.js"></script>
  <script src="/lib/animejs/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="/lib/@next-theme/pjax/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="/lib/@fancyapps/ui/dist/fancybox/fancybox.umd.js" integrity="sha256-q8XkJ6dj5VwSvzI8+nATCHHQG+Xv/dAZBCgqmu93zOY=" crossorigin="anonymous"></script>
  <script src="/lib/lozad/dist/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="/lib/hexo-generator-searchdb/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
